import request from '@/utils/request'

// 库存盘点相关接口
export const inventoryApi = {
  // 获取盘点列表
  getInventoryList(params: any) {
    return request({
      url: '/api/Inventory/GetInventoryList',
      method: 'post',
      data: params
    })
  },

  // 获取盘点详情
  getInventoryBySheetNo(sheetNo: string) {
    return request({
      url: '/api/Inventory/GetInventoryBySheetNo',
      method: 'post',
      data: { sheetNo }
    })
  },

  // 创建盘点单
  createInventory(data: any) {
    // 确保数据格式正确
    const cleanData = {
      sheetNo: data.sheetNo || '',
      type: data.type || '',
      startTime: data.startTime ? new Date(data.startTime).toISOString() : null,
      endTime: data.endTime ? new Date(data.endTime).toISOString() : null,
      warehouseName: data.warehouseName || '',
      handler: data.handler || '',
      remark: data.remark || '',
      rstauts: data.rstauts || 0,
      details: data.details ? data.details.map((detail: any) => ({
        goodsName: detail.goodsName || '',
        goodsCode: detail.goodsCode || '',
        spec: detail.spec || '',
        unit: detail.unit || '',
        goodsType: detail.goodsType || '',
        bookStock: detail.bookStock || 0,
        bookAmount: detail.bookAmount || 0,
        stockPosition: detail.stockPosition || '',
        actualQty: detail.actualQty || 0,
        diffQty: detail.diffQty || 0,
        checkStatus: detail.checkStatus || ''
      })) : []
    }

    return request({
      url: '/api/Inventory/CreateInventory',
      method: 'post',
      data: cleanData
    })
  },

  // 更新盘点单
  updateInventory(data: any) {
    // 确保数据格式正确
    const cleanData = {
      sheetNo: data.sheetNo || '',
      type: data.type || '',
      startTime: data.startTime ? new Date(data.startTime).toISOString() : null,
      endTime: data.endTime ? new Date(data.endTime).toISOString() : null,
      warehouseName: data.warehouseName || '',
      handler: data.handler || '',
      remark: data.remark || '',
      rstauts: data.rstauts || 0,
      details: data.details ? data.details.map((detail: any) => ({
        id: detail.id || 0,
        goodsName: detail.goodsName || '',
        goodsCode: detail.goodsCode || '',
        spec: detail.spec || '',
        unit: detail.unit || '',
        goodsType: detail.goodsType || '',
        bookStock: detail.bookStock || 0,
        bookAmount: detail.bookAmount || 0,
        stockPosition: detail.stockPosition || '',
        actualQty: detail.actualQty || 0,
        diffQty: detail.diffQty || 0,
        checkStatus: detail.checkStatus || ''
      })) : []
    }

    return request({
      url: '/api/Inventory/UpdateInventory',
      method: 'post',
      data: cleanData
    })
  },

  // 删除盘点单
  deleteInventory(sheetNo: string) {
    return request({
      url: '/api/Inventory/DeleteInventory',
      method: 'post',
      data: { sheetNo }
    })
  }
}

// 类型定义
export interface InventoryEntity {
  id?: number
  sheetNo: string
  type: string
  startTime: string | null
  endTime: string | null
  warehouseName: string
  handler: string
  remark: string
  rstauts: number
  details: DetailModel[]
}

export interface DetailModel {
  id?: number
  inventoryId?: number
  goodsName: string
  goodsCode: string
  spec: string
  unit: string
  goodsType: string
  bookStock: number
  bookAmount: number
  stockPosition: string
  actualQty: number
  diffQty: number
  checkStatus: string
}

export interface InventoryQueryDto {
  sheetNo?: string
  type?: string
  warehouseName?: string
  rstauts?: number
  pageIndex: number
  pageSize: number
}

export interface ApiResult<T> {
  success: boolean
  message: string
  data: T
  total?: number
} 